package com.czk.model.proxy.dynamincproxy.practice2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/10/2 19:57
 */
public class ProxyUtil {
    public static Object getProxy(Object target) {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new Inner(target));
    }

    private static class Inner implements InvocationHandler {
        private Object target;

        public Inner(Object target) {
            this.target = target;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("第二种方式开始代理了");
            method.invoke(target, args);
            System.out.println("第二种方式代理结束了");
            return proxy;
        }
    }
}

